package com.nd.mobile.study.domain;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.List;

import android.os.Bundle;

import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.annotations.SerializedName;
import com.fuckhtc.gson.reflect.TypeToken;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.config.Commands;
import com.nd.mobile.study.common.config.ReqParam;
import com.nd.mobile.study.common.constants.SharedPreferenceConstants;
import com.nd.mobile.study.common.constants.Tips;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.common.util.HttpRequestUtil;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.common.util.StringUtils;

/**
 *
 */
public class User implements Serializable {
    private static final long serialVersionUID = -5151302508106116740L;
    
    //默认用户注册类别
    public static final String USER_TYPE_ID = "1";
 
    @SerializedName("UserID")
    private String userId;
    
    @SerializedName("TrueName")
    private String realName;
    
    public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

    public static User register(String username, String password, String mobile, String email) throws Exception {
    	Bundle data = new Bundle();
    	data.putString(ReqParam.REGISTER_USERNAME, username);
    	data.putString(ReqParam.REGISTER_PASSWORD, password);
    	data.putString(ReqParam.REGISTER_USER_TYPE_ID, USER_TYPE_ID);
    	data.putString(ReqParam.REGISTER_MOBILE, mobile);
    	data.putString(ReqParam.REGISTER_EMAIL, email);
    	String jsonData = HttpRequestUtil.get(Commands.REGISTER, data);
    	Gson gson = new Gson();
		Type type = new TypeToken<Result<List<User>>>(){}.getType();
		Result<List<User>> result = gson.fromJson(jsonData, type);
    	User user = result.getBody().get(0);
    	if (null != user && (StringUtils.isBlank(user.getUserId())== false)) {
    		long userId = Long.parseLong(user.getUserId());
    		if(userId > 0){
    			App.getInstance().setUser(user);
    		}else{
    			throw new LocalErrorException(result.getPrompt());
    		}
    	} else {
    		throw new LocalErrorException(Tips.ERROR_ON_REGISTER);
    	}
    	return user;
    }

    public static User login(App app , String username, String password) throws Exception {
    	Bundle data = new Bundle();
    	data.putString(ReqParam.LOGIN_USERNAME, username);
    	data.putString(ReqParam.LOGIN_PASSWORD, password);
    	String jsonData = HttpRequestUtil.get(Commands.LOGIN, data);
		Gson gson = new Gson();
		Type type = new TypeToken<Result<List<User>>>(){}.getType();
		Result<List<User>> result = gson.fromJson(jsonData, type);
    	User user = result.getBody().get(0);
    	if (null != user && (StringUtils.isBlank(user.getUserId())== false)) {
    		long userId = Long.parseLong(user.getUserId());
    		if(userId > 0){
    			App.getInstance().setUser(user);
    		}else{
    			throw new LocalErrorException(result.getPrompt());
    		}
    	} else {
    		throw new LocalErrorException(Tips.ERROR_ON_LOGIN);
    	}
    	return user;
    }
    
    /**
     * 作用:将用户名与密码存入SharedPreferences 调用示例:
     * 
     * 参数:
     * @param app
     * @param username
     * @param password
     */
    public static void saveUserInfoIntoSharedPreferences(App app, String username, String password) {
        SharedPreferencesUtil shared = SharedPreferencesUtil.getUserInfoSp(app);
        shared.putValue(SharedPreferenceConstants.USERINFO_ACCOUNT, username);
        shared.putValue(SharedPreferenceConstants.USERINFO_PASSWORD, password);
    }
    
    /**
     * 作用:从SharedPreferences读取用户名与密码 调用示例:
     * 
     * <pre>
     * </pre>
     * 参数:
     * 
     * @param app
     * @return String[]{username,password}
     */
    public static String[] getUserInfoFromSharedPreferences(App app) {
        SharedPreferencesUtil sp = SharedPreferencesUtil.getUserInfoSp(app);
        String username = sp.getValue(SharedPreferenceConstants.USERINFO_ACCOUNT) == null ? "" : sp.getValue(SharedPreferenceConstants.USERINFO_ACCOUNT);
        String password = sp.getValue(SharedPreferenceConstants.USERINFO_PASSWORD) == null ? "" : sp.getValue(SharedPreferenceConstants.USERINFO_PASSWORD);
        return new String[] { username, password };
    }
    
    public static void clearUserInfoSharedPreferences(App app){
    	SharedPreferencesUtil sp = SharedPreferencesUtil.getUserInfoSp(app);
        sp.putValue(SharedPreferenceConstants.USERINFO_ACCOUNT,null);
        sp.putValue(SharedPreferenceConstants.USERINFO_PASSWORD,null);
    }

    private <T> T get(String command) throws Exception {
    	Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(command, data);
		Result<T> result = new Gson().fromJson(jsonData, new TypeToken<Result<T>>(){}.getType());
		T body = result.getBody();
		return body;
    }
    
	public ClassInfo[] getClassInfo() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(Commands.GET_CLASS_INFO, data);
		Result<ClassInfo[]> result = new Gson().fromJson(jsonData, new TypeToken<Result<ClassInfo[]>>(){}.getType());
		ClassInfo[] body = result.getBody();
		return body;
	}
	
	public AttentionCatalog[] getAttentionInfo() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(Commands.GET_ATTENTION_CATALOG, data);
		Result<AttentionCatalog[]> result = new Gson().fromJson(jsonData, new TypeToken<Result<AttentionCatalog[]>>(){}.getType());
		AttentionCatalog[] body = result.getBody();
		return body;
	}
	
	public VideoRecord[] getVideoRecord() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(Commands.GET_VIDEO_RECORD_CATALOG, data);
		Result<VideoRecord[]> result = new Gson().fromJson(jsonData, new TypeToken<Result<VideoRecord[]>>(){}.getType());
		VideoRecord[] body = result.getBody();
		return body;
	}
	
	public BluebookRecord[] getBluebookRecord() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(Commands.GET_EXAM_A_CATALOG, data);
		Result<BluebookRecord[]> result = new Gson().fromJson(jsonData, new TypeToken<Result<BluebookRecord[]>>(){}.getType());
		BluebookRecord[] body = result.getBody();
		return body;
	}
	
	public HomeworkARecord[] getHomeworkARecord() throws Exception {
		Bundle data = new Bundle();
		data.putString(ReqParam.COMMON_USER_ID, this.userId);
		String jsonData = HttpRequestUtil.get(Commands.GET_HOMEWORK_A_CATALOG, data);
		Result<HomeworkARecord[]> result = new Gson().fromJson(jsonData, new TypeToken<Result<HomeworkARecord[]>>(){}.getType());
		HomeworkARecord[] body = result.getBody();
		return body;
	}
	
}
